<?php

namespace Api\Controller;

class PostController extends ApiController {

    private $token;
    private $deviceid;
    private $user_log;
    private $user_info;
    private $user_id;
    private $error_post;

    function __construct() {
        parent::__construct();
        $this->token = I('request.token');
        $this->deviceid = I('request.deviceid');
        $this->user_info = $this->verify_token($this->token, $this->deviceid);
        $this->user_id = $this->user_info['user_id'];
        $this->error_post = L('error_post');
    }

    private function is_power($user_id, $class_id, $power) {
        $Member = D('Member');
        $member = $Member->getMembyids($user_id, $class_id);
        if (empty($member['power']) || $member['power'] < $power) {
            return FALSE;
        } else {
            return TRUE;
        }
    }

    function addpost() {
        $power = 3;
        $class_id = I('request.classid');
        $media = '';
        switch (is_array($class_id)) {
            case 0:
                if (!$this->is_power($this->user_id, $class_id, $power)) {
                    $this->error($this->error_post['powerdown']);
                }
                $post_content = I('request.content', '', 'nl2br,htmlspecialchars');
                if (strlen($post_content) < 1) {
                    $this->error($this->error_post['nopost']);
                }
                if (strlen($post_content) > 200) {
                    $post_excerpt = substr(strip_tags(htmlspecialchars_decode($post_content)), 0, 200);
                } else {
                    $post_excerpt = strip_tags(htmlspecialchars_decode($post_content));
                }
                $imgs = I('request.img') != FALSE ? I('request.img') : '';
                if (!empty($imgs)) {
                    $images = explode(',', $imgs);
                    foreach ($images as $key => $value) {
                        $value = str_replace('//', '/', $value);
                        $media['img'][] = $value;
                    }
                    $media = json_encode($media);
                }
                $is_vote = I('request.isvote') != FALSE ? I('request.isvote') : 0;
                $is_comment = I('request.iscomment') != FALSE ? I('request.iscomment') : 0;
                $Post = D('Post');
                $pid = $Post->addPost($class_id, $this->user_id, $post_excerpt, $post_content, $media, $is_vote, $is_comment);
                if ($pid) {
                    $this->warnclass($this->user_id, $class_id);
                    $arrInfo = array('id' => $pid);
                    $this->success(__FUNCTION__, $arrInfo);
                } else {
                    $this->error($this->error_post['oserror']);
                }
                break;
            case 1:
                foreach ($class_id as $value) {
                    if (!$this->is_power($this->user_id, $value, $power)) {
                        $this->error($this->error_post['powerdown']);
                    }
                    $post_content = I('request.content', '', 'nl2br,htmlspecialchars');
                    if (strlen($post_content) < 1) {
                        $this->error($this->error_post['nopost']);
                    }
                    if (strlen($post_content) > 200) {
                        $post_excerpt = substr($post_content, 0, 200);
                    } else {
                        $post_excerpt = $post_content;
                    }
                    $imgs = I('request.img') != FALSE ? I('request.img') : '';
                    if (!empty($imgs)) {
                        $images = explode(',', $imgs);
                        foreach ($images as $key => $value) {
                            $value = str_replace('//', '/', $value);
                            $media['img'][] = $value;
                        }
                        $media = json_encode($media);
                    }
                    $media = json_encode($media);
                    $is_vote = I('request.isvote') != FALSE ? I('request.isvote') : 0;
                    $is_comment = I('request.iscomment') != FALSE ? I('request.iscomment') : 0;
                    $Post = D('Post');
                    $pid = $Post->addPosts($value, $this->user_id, $post_excerpt, $post_content, $media, $is_vote, $is_comment);
                    if ($pid) {
                        $this->warnclass($this->user_id, $value);
                        $arrInfo = array('id' => $pid);
                        $this->success(__FUNCTION__, $arrInfo);
                    } else {
                        $this->error($this->error_post['oserror']);
                    }
                }
                break;
            default :
                $this->error($this->error_post['powerdown']);
                break;
        }
    }

    function getposts() {
        $limit = 20;
        $start = I('request.start') != FALSE ? I('request.start') : 0;
        $Member = D('Member');
        $classes = $Member->getClassesbyUserid($this->user_id);
        foreach ($classes as $value) {
            $cids[] = $value['class_id'];
        }
        $info = array();
        if (count($cids) >= 1) {
            $Post = D('Post');
            $post = $Post->getPostlistbycids($cids, $start, $limit);
            foreach ($post as $key => $value) {
                $value['time'] = timetodate($value['createtime']);
                $post[$key] = $value;
            }
            $info['posts'] = $post;
            $info['page'] = $start * $limit + $limit;
            $this->success(__FUNCTION__, $info);
        } else {
            $this->success(__FUNCTION__, $info);
        }
    }

    function getpostbypid() {
        $post_id = I('request.postid');
        $class_id = I('request.classid');
        $Member = D('Member');
        $member = $Member->getMembyids($this->user_id, $class_id);
        if (!$member['id'] || empty($member['id']) || ($member['power'] < 1)) {
            $this->error($this->error_post['noinclass']);
        }
        $Post = D('Post');
        $post = $Post->getPostbypid($post_id);
        if (!$post['class_id'] || empty($post['class_id']) || $post['class_id'] != $class_id) {
            $this->error($this->error_post['oserror']);
        }
        $post['content'] = htmlspecialchars_decode($post['content']);
        $info = array();
        $media = (array) json_decode($post['media']);
        if ($media['img'] && (!empty($media['img']))) {
            $url = C('IMAGEURL');
            $imgs = $media['img'];
            foreach ($imgs as $key => $value) {
                $imgs[$key] = $url . $value;
            }
            $post['media'] = $imgs;
        } else {
            unset($post['media']);
        }
        $info['post'] = $post;
        if ($post['is_vote']) {
            $info['vote'];
        }
        $Reply = D('Reply');
        $reply = $Reply->getCommentbyIds($post['id'], $this->user_id);
        if ($reply) {
            $info['icon'] = $reply['icon'];
        }
        if ($this->user_id == $post['user_id'] || $member['power'] == 10) {
            $info['icons'] = $Reply->getIcons($post['id']);
        }
        $this->success(__FUNCTION__, $info);
    }

    function getpostsbycid() {
        $limit = 20;
        $class_id = I('request.classid');
        $start = I('request.start') != FALSE ? I('request.start') : 0;
        $Member = D('Member');
        $member = $Member->getMembyids($this->user_id, $class_id);
        if (!$member['id'] || empty($member['id']) || ($member['power'] < 1)) {
            $this->error($this->error_post['noinclass']);
        }
        $info = array();
        $Post = D('Post');
        $post = $Post->getPostlistbycid($class_id, $start, $limit);
        foreach ($post as $key => $value) {
            $value['time'] = timetodate($value['createtime']);
            $post[$key] = $value;
        }
        $info['posts'] = $post;
        if ($start == 0 && count($post) < $limit) {
            $info['page'] = count($post);
        } else {
            $info['page'] = $start * $limit + $limit;
        }
        $this->success(__FUNCTION__, $info);
    }

    protected function warnclass($iUid, $iCid) {
        $iLimit = 30;
        $mCmember = D('Member');
        $mClass = D('Class');
        $aClass = $mClass->getClassbycid($iCid);
        $sCname = $aClass['classname'];
        $aCmem = $mCmember->getMembyids($iUid, $iCid);
        $sRename = $aCmem['rename'];
        $sContent = $sRename . '在班级' . $sCname . '中发布了一条信息';
        $iCnum = $mCmember->getCountbycid($iCid);
        if ($iCnum <= $iLimit) {
            $iStart = 0;
            $aMembers = $mCmember->getuList($iUid, $iCid, $iStart, $iLimit);
            foreach ($aMembers as $aVal) {
                $iUids[] = $aVal['user_id'];
            }
        } else {
            $iNum = floor($iCnum / $iLimit);
            $iStart = 0;
            for ($iN = $iNum; $iN >= 0; $iN--) {
                $aMembers = $mCmember->getuList($iUid, $iCid, $iStart, $iLimit);
                foreach ($aMembers as $aVal) {
                    $iUids[] = $aVal['user_id'];
                }
                $iStart = $iStart * $iLimit + $iLimit;
            }
        }
        if (!$iUids || empty($iUids)) {
            return;
        } else {
            $this->jpushapi($iUids, $sContent, '', 1);
        }
    }

}
